package com.example.idachuappone.index.alipay;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONObject;

import android.util.Log;

public class Result {
	private static final Map<String, String> sError;
	public static String sResult;

	static {
		sError = new HashMap();
		sError.put("9000", "\u64cd\u4f5c\u6210\u529f");
		sError.put("4000", "\u7cfb\u7edf\u5f02\u5e38");
		sError.put("4001", "\u6570\u636e\u683c\u5f0f\u4e0d\u6b63\u786e");
		sError.put(
				"4003",
				"\u8be5\u7528\u6237\u7ed1\u5b9a\u7684\u652f\u4ed8\u5b9d\u8d26\u6237\u88ab\u51bb\u7ed3\u6216\u4e0d\u5141\u8bb8\u652f\u4ed8");
		sError.put("4004", "\u8be5\u7528\u6237\u5df2\u89e3\u9664\u7ed1\u5b9a");
		sError.put("4005", "\u7ed1\u5b9a\u5931\u8d25\u6216\u6ca1\u6709\u7ed1\u5b9a");
		sError.put("4006", "\u8ba2\u5355\u652f\u4ed8\u5931\u8d25");
		sError.put("4010", "\u91cd\u65b0\u7ed1\u5b9a\u8d26\u6237");
		sError.put("6000", "\u652f\u4ed8\u670d\u52a1\u6b63\u5728\u8fdb\u884c\u5347\u7ea7\u64cd\u4f5c");
		sError.put("6001", "\u7528\u6237\u4e2d\u9014\u53d6\u6d88\u652f\u4ed8\u64cd\u4f5c");
		sError.put("7001", "\u7f51\u9875\u652f\u4ed8\u5931\u8d25");
	}

	public static String getResult() {
		return getContent(sResult.replace("{", "").replace("}", ""), "memo=", ";result");
	}

	public static boolean isSucc() {
		if (sResult == null) {
			return false;
		}
		return "9000".equals(getContent(sResult.replace("{", "").replace("}", ""), "resultStatus=", ";memo"));
	}

	public static void parseResult() {
		try {
			String resultStatus;
			String src = sResult.replace("{", "").replace("}", "");
			String rs = getContent(src, "resultStatus=", ";memo");
			if (sError.containsKey(rs)) {
				resultStatus = sError.get(rs);
			} else {
				resultStatus = "\u5176\u4ed6\u9519\u8bef";
			}
			resultStatus = new StringBuilder(String.valueOf(resultStatus)).append("(").append(rs).append(")")
					.toString();
			String memo = getContent(src, "memo=", ";result");
			boolean isSignOk = checkSign(getContent(src, "result=", null));
			Log.e("aaaa_pay", "resultStatus-->" + resultStatus);
			Log.e("aaaa_pay", "memo-->" + memo);
			Log.e("aaaa_pay", "isSignOk-->" + isSignOk);
		} catch (Exception e) {
		}
	}

	private static boolean checkSign(String result) {
		boolean retVal = false;
		try {
			JSONObject json = string2JSON(result, "&");
			String signContent = result.substring(0, result.indexOf("&sign_type="));
			String signType = json.getString("sign_type").replace("\"", "");
			String sign = json.getString("sign").replace("\"", "");
			if (signType.equalsIgnoreCase("RSA")) {
				retVal = Rsa.doCheck(signContent, sign, Keys.PUBLIC);
			}
		} catch (Exception e) {
		}
		return retVal;
	}

	public static JSONObject string2JSON(String src, String split) {
		JSONObject json = new JSONObject();
		try {
			String[] arr = src.split(split);
			for (int i = 0; i < arr.length; i++) {
				String[] arrKey = arr[i].split("=");
				json.put(arrKey[0], arr[i].substring(arrKey[0].length() + 1));
			}
		} catch (Exception e) {
		}
		return json;
	}

	private static String getContent(String src, String startTag, String endTag) {
		String content = src;
		int start = src.indexOf(startTag) + startTag.length();
		try {
			return endTag != null ? src.substring(start, src.indexOf(endTag)) : src.substring(start);
		} catch (Exception e) {
			return content;
		}
	}
}
